package cn.kgc.crowdfundingadminspringboot.controller;



import cn.kgc.crowdfundingadminspringboot.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadminspringboot.common.ResultEntity;

import cn.kgc.crowdfundingadminspringboot.pojo.Admin;
import cn.kgc.crowdfundingadminspringboot.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    AdminService adminService;

    @RequestMapping("/to/login/page.html")
    public String toLoginPage() {
        return "admin-login";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index";
    }

    /**
     * 跳转后台主页面
     * @return
     */
    @RequestMapping("/to/main/page.html")
    public String toLogin(){
        return "admin-main";
    }

    /**
     * 登录
     */
    @RequestMapping("/do/login")
    public String doLogin(
            @RequestParam("loginAcct") String loginAcct,
            @RequestParam("userPswd") String userPswd,
            Model model, HttpSession session){
        Admin admin = adminService.login(loginAcct, userPswd);
        if (admin==null){
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }
        session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN, admin);
        return "redirect:/admin/to/main/page.html";
    }

    /**
     * 查询用户
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @param model
     * @return
     */
    @RequestMapping("/query/for/search")
    public String queryForSearch(
            // 如果页面上没有提供对应的请求参数，那么可以使用defaultValue指定默认值
            @RequestParam(value="pageNum", required = false,defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize",required = false,defaultValue="5") Integer pageSize,
            @RequestParam(value="keyword", required = false,defaultValue="") String keyword,
            Model model) {

        PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);

        return "admin-page";
    }

    /**
     * 删除
     * @param adminIdList
     * @return
     */
    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList) {

        try {

            adminService.batchRemove(adminIdList);

            return ResultEntity.successWithoutData();
        }catch(Exception e) {

            return ResultEntity.failed(e.getMessage());
        }

    }

    @RequestMapping("/to/add/page.html")
    public String add(){

        return "admin-add";
    }


    /**
     * 添加用户
     * @param admin
     * @return
     */
    @RequestMapping("/save")
    public String saveAdmin(Admin admin) {

        adminService.saveAdmin(admin);

        return "redirect:/admin/query/for/search";
    }

    /**
     * 获取用户信息，跳转修改页面
     */
    @RequestMapping("/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {

        Admin admin = adminService.getAdminById(adminId);

        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN, admin);

        return "admin-edit";
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public String updateAdmin(Admin admin,Integer pageNum) {
        try {
            adminService.updateAdmin(admin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }

}
